using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace IBuyAdventure
{
   public class UsersDB
   {

   private string m_ConnectionString;

   public UsersDB( string dsn ) {
      m_ConnectionString = dsn; 
   }

   public void AddNewUser(string customerName, string password) {

          String insertStatement = "INSERT INTO Accounts (CustomerName, Password) values ('" + customerName + "', '" + password + "')";

          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlCommand myCommand = new SqlCommand(insertStatement, sqlConnection);

          myCommand.Connection.Open();
          myCommand.ExecuteNonQuery();
          myCommand.Connection.Close();
      }      
  
  public bool UserExists(string customerName){
          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("SELECT CustomerName FROM Accounts WHERE CustomerName='"+customerName+"'", sqlConnection);

          DataSet accountDetails = new DataSet();
          sqlAdapter1.Fill(accountDetails, "accountDetails");

          if (accountDetails.Tables[0].Rows.Count < 1) {
            return false;
          }
		  else
		  	return true;
          
}
      
      public bool ValidateLogin(String customerName, String password) {
      
          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("SELECT * FROM Accounts WHERE CustomerName='"+customerName+"'", sqlConnection);

          DataSet accountDetails = new DataSet();
          sqlAdapter1.Fill(accountDetails, "accountDetails");

          if (accountDetails.Tables[0].Rows.Count < 1) {
            return false;
          }
          
          return (String.Compare(password.Trim(), ((String) accountDetails.Tables[0].Rows[0]["Password"]).Trim()) == 0);
      }
   }

}


